pimpas.Utils = Class.create( );
pimpas.Extends(pimpas.Utils, pimpas.Object);

pimpas.Utils.DefaultListener = Class.create( );
pimpas.Extends(pimpas.Utils.DefaultListener.prototype, pimpas.Listener.prototype);
pimpas.Extends(pimpas.Utils.DefaultListener.prototype,
	{
		element: null,
		initialize: function(element) {
			this.id = pimpas.globalID++;
			this.log = new pimpas.Log();
			this.element = element;	
		}
	}
);

pimpas.Extends(pimpas.Utils,
	{
		disableFocus: function() {
			var el = null;
			for(var i=0;i<arguments.length;i++) {
				el = pimpas.DOM.getElement( arguments[i] );
				if(!pimpas.DOM.isDefined(el)) {
					this.log.error("Can't find element with id/name ["+arguments[i]+"]");
					continue;
				}
				this.log.debug("[Disabling focus for Element {"+arguments[i]+"}]");
				var listener = new pimpas.Utils.DefaultListener(el);
				pimpas.Extends(listener,
					{
						callBack: function(event) {
							event = pimpas.DOM.Event.fixEvent(event);
							// give focus to next child
							pimpas.DOM.Event.stop(event);
							this.element.blur();
							var parent = this.element.parentNode;
							if(parent == null)
								return;
							
							// give focus to next child
							var child = null;
							var found = false;
							for(var i=0;i<parent.childNodes.length;i++) {
								if(found) {
									if(pimpas.DOM.isField(parent.childNodes[i])) {
										child = parent.childNodes[i];
										break;
									}
								}
								if(parent.childNodes[i] == this.element) {
									found = true;
									continue;
								}
							}
							
							if(child == null) return;
							
							pimpas.DOM.focus( child );
						}
					}
				);
				var func = function(event) { listener.callBack(event); };
				pimpas.DOM.Event.addListener( el, pimpas.DOM.Event.FOCUS, func, true);
				
				return listener;
			}
		}
	}
);

pimpas.Utils.initialize( );